package com.crx.springbootinit.controller.user;


import com.crx.springbootinit.annotation.LogApiAnno;
import com.crx.springbootinit.common.BaseResponse;
import com.crx.springbootinit.common.ResultUtils;
import com.crx.springbootinit.model.dto.comment.CommentDto;
import com.crx.springbootinit.model.dto.comment.CommentPostIdPageDTO;
import com.crx.springbootinit.model.dto.comment.CommentRootIdPageDTO;
import com.crx.springbootinit.model.dto.page.PageVO;
import com.crx.springbootinit.model.entity.Comment;
import com.crx.springbootinit.model.vo.comment.CommentChildrenVO;
import com.crx.springbootinit.model.vo.comment.CommentsVO;
import com.crx.springbootinit.model.vo.comment.NoticeCommentVO;
import com.crx.springbootinit.service.ICommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/user/comment")
@Api(tags = "评论模块接口")
public class CommentController {
    @Autowired
    private ICommentService commentService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增评论
     *
     * @param commentDto 评论请求对象
     * @return 成功消息
     */
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation("新增评论")
    @LogApiAnno(title = "评论模块", businessType = "POST")
    public BaseResponse addComment(@RequestBody CommentDto commentDto) {
        CommentChildrenVO commentChildrenVO = commentService.addComment(commentDto);
        return ResultUtils.success(commentChildrenVO);
    }

    /**
     * 根据关联的主题ID查评论列表
     *
     * @param commentPostIdPageDTO
     * @return 树形结构的列表
     */
    @GetMapping("/list")
    @ApiOperation("根据帖子Id获取帖子评论")
    @LogApiAnno(title = "评论模块", businessType = "GET")
    public BaseResponse<PageVO<CommentsVO>> getCommentList(HttpServletRequest request, CommentPostIdPageDTO commentPostIdPageDTO) {
        String token = request.getHeader("token");
        PageVO<CommentsVO> pb = commentService.getCommentListByPostId(token,commentPostIdPageDTO);
        return ResultUtils.success(pb);
    }

    @GetMapping("/getChildrenList")
    @ApiOperation("根据帖子Id和根评论Id获取帖子评论")
    @LogApiAnno(title = "评论模块", businessType = "GET")
    public BaseResponse<PageVO<CommentChildrenVO>> getChildrenList(HttpServletRequest request,CommentRootIdPageDTO commentRootIdPageDTO) {
        String token = request.getHeader("token");
        Long userId = new Long(0);
        if(!StringUtils.isEmpty(token)){
            userId = (Long) redisTemplate.opsForValue().get(token);
        }
        int offset = (commentRootIdPageDTO.getPageNo() - 1) * 5;
        PageVO<CommentChildrenVO> pb = commentService.getChildrenComments(userId,commentRootIdPageDTO.getRootCommentId(),commentRootIdPageDTO.getPostId(),5,offset);
        return ResultUtils.success(pb);
    }

    /**
     * 评论回复通知列表
     * @return 树形结构的列表
     */
    @GetMapping("/noticeList")
    @ApiOperation("评论回复通知列表")
    @LogApiAnno(title = "评论模块", businessType = "GET")
    public BaseResponse<PageVO<NoticeCommentVO>> getNoticeCommentList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) {
        PageVO<NoticeCommentVO> noticeCommentList = commentService.getNoticeCommentList(pageNum,pageSize);
        return ResultUtils.success(noticeCommentList);
    }
}
